<?php
	define('MIS_SUB','NetmisApi.getBtoSubCatalog');
	define('MIS_LIST','NetmisApi.getBtoAppService');
	define('MIS_APP','NetmisApi.getServiceListByApplicationId');
	define('MIS_IP','netmis.gamebto.com');
	define('MIS_URL','/netmisapi');
	define('MIS_PORT',80);
	define('frg_bak_task_dir','/data/www/EBAK/tasks/frg');
	include("./include/xmlrpc/lib/xmlrpc.inc");
	$xmlrpc_internalencoding = 'UTF-8';
	include_once './config.php';
	include_once './include/ez_sql_core.php';
	include_once './include/ez_sql_mysql.php';
	$db = new ezSQL_mysql($config['db']['dbuser'],$config['db']['dbpw'],$config['db']['dbname'],$config['db']['dbhost']);
	$db->query('set names '.$config['db']['dbcharset']);
	function object_array($array){
		if(is_object($array)){
			$array = (array)$array;
		}
		if(is_array($array)){
			foreach($array as $key=>$value){
				$array[$key] = object_array($value);
			}
		}
		return $array;
	}
    function icon_change($result){
        $row = array();
        $row['id'] = $result->id;
        $row['name'] = $result->name;
        $row['privileges']= $result->privileges;
        $row['create_time']= $result->create_time;
        $row['login_time']= $result->login_time;
        $privileges = json_decode($result->privileges);
        if ($privileges->system->admin){
            $row['icon']= 'admin.png';
        }else if (!empty($privileges->system->index)){
            foreach ($privileges->system->index as $index){
                if($privileges->$index->admin){
                    if ($privileges->system->login == 1){
                        $row['icon']= 'half_admin.png';
                        break;
                    }else{
                        $row['icon']= 'half_admin_stop.png';
                        break;
                    }	
                }else{
                    if ($privileges->system->login == 1){
                        $row['icon']= 'user.png';
                        break;
                    }else{
                        $row['icon']= 'user_stop.png';
                        break;
                    }
                }
            }
        }else{
            $row['icon']= 'no_pri_user.png';
        }
        return $row;
    }
	switch($_REQUEST['type'])
	{
		case 'login':
			session_start();
			extract($_REQUEST);
			if($save == 'true') {
				setcookie(session_name(), session_id(), time() + 24 * 3600, "/");
            }
            if (!empty($user)){
             	$db2 = new ezSQL_mysql('netmis','dVnetmis','netmis','121.10.140.91:3307');
				$db2->query('set names utf8');
				$sql = "SELECT uid FROM user WHERE username = '".$user."' AND password = '".md5($pass)."' LIMIT 1";
				$result2=$db2->get_row($sql);
				if ($db2->num_rows > 0){
					$db->query("update user set login_time = now() where id = ".$result2->uid);
					//$sql = "SELECT name,privileges FROM user WHERE id='".$result2->uid."' AND status = 1";
					$sql = "SELECT name,privileges FROM user WHERE id=".$result2->uid;
					$result=$db->get_row($sql);
					if ($db->num_rows > 0){
                        $privileges = json_decode($result->privileges);
                        if ($privileges->system->login){
                            $_SESSION['id'] = $result2->uid;
                            $_SESSION['name'] = $result->name;
                            $_SESSION['privileges'] = $privileges;
                            echo "success";
                            die();
                        }else{
                            echo "fail";
                            die();
                        }
					}else{
						echo "fail";
						die();
					}
				}else{
					echo "fail";
					die();
				}
             }else{
             	echo "fail";
				die();
             }
        break;
		case 'has_login':
			session_start();
			if($_SESSION['id'] != '') {
				$db->query("update user set login_time = now() where id = ".$_SESSION['id']);
				if ($db->rows_affected > 0){
					echo "success";
					die();
				}else{
					echo "Fail";
					die();
				}
			}else{
				echo "Fail";
				die();
			}
		break;
		case 'userinfo':
			session_start();
			if($_SESSION['id'] != '') {
				$sql = "SELECT name,privileges,create_time,login_time FROM user where id = ".$_SESSION['id'];
				$result = $db->get_row($sql);
				if ($db->num_rows > 0){
					$output = array('id'=>$_SESSION['id'],'name'=>$result->name,'privileges'=>$result->privileges,'create_time'=>$result->create_time,'login_time'=>$result->login_time);
				}
			}
			header("Cache-Control: no-cache");
			echo json_encode(array('userinfo'=>$output));
		break;
		case 'get_user_info':
			session_start();
			$output = array();
			if($_SESSION['id'] != '') {
				array_push($output, array('id' => $_SESSION['id'], 'name' => $_SESSION['name']));
			}else{
				array_push($output, array('id' => 'ERROR', 'name' => 'ERROR'));
			}
			header("Cache-Control: no-cache");
			echo json_encode($output);
		break;
        case 'get_user_pri':
            session_start();
            extract($_REQUEST);
            $output = array();
            if($_SESSION['id'] != '') {
                if(($_SESSION['privileges']->system->admin)||($_SESSION['privileges']->$index->admin)){
                    $result = $db->get_var("select privileges from user where id = ".$id);
                    if (!empty($result)){
                        $result = json_decode($result);
                        $output = array('index'=>$index,'pri'=>json_encode($result->$index));
                    }
                }
            }
            header("Cache-Control: no-cache");
			echo json_encode(array('getuserpri'=>$output));
        break;
        case 'logout':
			session_start();
			session_destroy();
			echo "success";
			die();
		break;
        case 'add_user_list':
			session_start();
			if ($_SESSION['id'] != ''){
				$db2 = new ezSQL_mysql('netmis','dVnetmis','netmis','121.10.140.91:3307');
				$db2->query('set names utf8');
				$sql = "SELECT id FROM user";
				$result = array();
				foreach ($db->get_col($sql) as $tmp){
					array_push($result,$tmp);
				}
				$sql = "SELECT uid,nickname FROM user";
				$output = array();
				foreach ($db2->get_results($sql) as $result2){
					if (!in_array($result2->uid,$result)){
						array_push($output,array('id'=>$result2->uid,'name'=>$result2->nickname));
					}
				}
			}
			header("Cache-Control: no-cache");
            echo json_encode(array('add_user_list'=>$output));	
		break;
		case 'add_user':
			session_start();
			extract($_REQUEST);
			if($_SESSION['id'] != ''){
                $privileges = '{"system":{"login":false,"admin":false,"index":[]}}';
				$db->query("insert into user (id,name,privileges,create_time) VALUES (".$id.",'".$name."','".$privileges."',now())");
				if ($db->rows_affected > 0){
					echo json_encode(array('success'=>true,'msg'=>'success'));
					die();
				}else{
					echo json_encode(array('success'=>false,'msg'=>'fail'));
					die();
				}
			}else{
				echo json_encode(array('success'=>false,'msg'=>'fail'));
				die();
			}
		break;
		case 'delete_user':
			session_start();
			extract($_REQUEST);
			if($_SESSION['id'] != ''){
				$db->query("delete from user where id = ".$id);
				if ($db->rows_affected > 0){
					echo "success";
					die();
				}else{
					echo "fail";
					die();
				}
			}else{
				echo "fail";
				die();
			}
		break;
        case 'enable_user':
			session_start();
			extract($_REQUEST);
			if($_SESSION['id'] != ''){
                $result = $db->get_var("select privileges from user where id = ".$id);
                if (!empty($result)){
                    $result = json_decode($result);
                    $result->system->login = true;
                    $db->query("update user set privileges = '".json_encode($result)."' where id = ".$id);
                }
				if ($db->rows_affected > 0){
					echo "success";
					die();
				}else{
					echo "fail";
					die();
				}
			}else{
				echo "fail";
				die();
			}
		break;
		case 'disable_user':
			session_start();
			extract($_REQUEST);
			if($_SESSION['id'] != ''){
                $result = $db->get_var("select privileges from user where id = ".$id);
                if (!empty($result)){
                    $result = json_decode($result);
                    $result->system->login = false;
                    $db->query("update user set privileges = '".json_encode($result)."' where id = ".$id);
                }
				if ($db->rows_affected > 0){
					echo "success";
					die();
				}else{
					echo "fail";
					die();
				}
			}else{
				echo "fail";
				die();
			}
		break;
        case 'system_settings_user_list':
            session_start();
            if($_SESSION['id'] != ''){
                $results=$db->get_results("select * from user");
                if($db->num_rows > 0){
                    $output = array();
                    if($_SESSION['privileges']->system->admin){
                        foreach ($results as $result){
                            array_push($output,icon_change($result));
                        }
                    }else{
                        $privileges = array();
                        foreach($_SESSION['privileges']->system->index as $index){
                            if($_SESSION['privileges']->$index->admin){
                                array_push($privileges,$index);
                            }
                        }
                        foreach($results as $result){
                            $tmp = json_decode($result->privileges);
                            if(!empty($tmp)){
                                foreach($tmp->system->index as $index){
                                   if(in_array($index,$privileges)&&(!$tmp->$index->admin)&&(!$tmp->system->admin)||($_SESSION['id']==$result->id)){
                                        array_push($output,icon_change($result));
                                   } 
                                }
                            }
                        }
                    }
                header("Cache-Control: no-cache");
                echo json_encode(array('system_settings_user_list' => $output));
                }
            }
		break;
        case 'save_user_pri':
            session_start();
			extract($_REQUEST);
            if(($_SESSION['id'] != '')&&(!empty($id))){
                //if($_SESSION['privileges']->system->admin){
                    $db->query("update user set privileges = '".$pri."' where id = ".$id);
					if ($db->rows_affected > 0){
						echo 'success';
						die();
					}
                //}else{
					//die();
				//}
            }
        break;
        case 'frg_bak_tree':
				session_start();
				extract($_REQUEST);
				if ($node=='root'){
					$id = '0002005M';
					$f=new xmlrpcmsg(MIS_SUB,array(php_xmlrpc_encode($id)));
					$c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
					$r=&$c->send($f);
					if(!$r->faultCode()) {
                		$v=php_xmlrpc_decode($r->value());
						$output = array();
                		foreach($v as $row) {
							if (in_array($row[0],$_SESSION['privileges']->frgbak->lang)){
								array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false));
							}
               		 	}
					} else {
                			echo 'fail';
					}
				}elseif (!empty($node) && (strlen($node)< 16)){
					$id = $node;
					$f=new xmlrpcmsg(MIS_SUB,array(php_xmlrpc_encode($id)));
					$c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
					$r=&$c->send($f);
					if(!$r->faultCode()) {
                		$v=php_xmlrpc_decode($r->value());
						if(empty($v)){
							$f=new xmlrpcmsg(MIS_LIST,array(php_xmlrpc_encode($id)));
							$c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
							$r=&$c->send($f);
							if(!$r->faultCode()) {
								$v=php_xmlrpc_decode($r->value());
							}
						}
						$output = array();
                		foreach($v as $row) {
							if (strlen($row[0]) >= 16){
								array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false,'leaf' => true));
							}elseif(in_array($row[0],$_SESSION['privileges']->frgbak->partner)){
								array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false));
							}
                		}
					} else {
                		echo 'fail';
					}
				}
				header("Cache-Control: no-cache");
        		echo json_encode($output);
        break;
        case 'frgbak_landp_tree':
				session_start();
				extract($_REQUEST);
                $userpri = $db->get_var("select privileges from user where id = ".$id);
                if (!empty($userpri)){
                    $userpri = json_decode($userpri);
                }
                $output = array();
                if(!empty($userpri->system->index)){
                    $tmp = in_array('frgbak',$userpri->system->index,false);
                }else{
                    $tmp = false;
                }
                if ($_SESSION['privileges']->system->admin || $tmp){
                    if ($node=='root'){
                        $root = '0002005M';
                        $f=new xmlrpcmsg(MIS_SUB,array(php_xmlrpc_encode($root)));
                        $c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
                        $r=&$c->send($f);
                        if(!$r->faultCode()) {
                            $v=php_xmlrpc_decode($r->value());
                            foreach($v as $row) {
                                if ($tmp){
                                   if (in_array($row[0],$userpri->frgbak->lang)){
                                        array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => true));
                                    }else{
                                        array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false));
                                    } 
                                }else{
                                    array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false));
                                }
                            }
                        } else {
                                echo 'fail';
                        }
                    }elseif (!empty($node) && (strlen($node)< 16)){
                        $f=new xmlrpcmsg(MIS_SUB,array(php_xmlrpc_encode($node)));
                        $c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
                        $r=&$c->send($f);
                        if(!$r->faultCode()) {
                            $v=php_xmlrpc_decode($r->value());
                            if(empty($v)){
                                $f=new xmlrpcmsg(MIS_LIST,array(php_xmlrpc_encode($id)));
                                $c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
                                $r=&$c->send($f);
                                if(!$r->faultCode()) {
                                    $v=php_xmlrpc_decode($r->value());
                                }
                            }						
                            foreach($v as $row) {
                                if (strlen($row[0]) < 16){
                                    if ($tmp){
                                        if (in_array($row[0],$userpri->frgbak->partner)){
                                            array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => true,'leaf' => true));
                                        }else{
                                            array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false,'leaf' => true));
                                        }
                                    }else{
                                        array_push($output, array('text' => $row[1], 'id' => $row[0], 'act' => 'app', 'checked' => false,'leaf' => true));
                                    }
                                }
                            }
                        } else {
                            echo 'fail';
                        }
                    }
                }
				header("Cache-Control: no-cache");
        		echo json_encode($output);
        break;
		case 'frg_bak_newtask':
			session_start();
			if($_SESSION['id'] != '') {
				extract($_REQUEST);
				$output = '';
				$output_tmp = array();
				foreach(json_decode(stripslashes($taskid)) as $tmp){
					$task = object_array($tmp);
					$f=new xmlrpcmsg(MIS_APP,array(php_xmlrpc_encode($task['id'])));
					$c=new xmlrpc_client(MIS_URL, MIS_IP, MIS_PORT);
					$r=&$c->send($f);
					if(!$r->faultCode()) {
						$v=php_xmlrpc_decode($r->value());
						foreach($v as $row) {
							if ($row['serviceType']=='db'){
								$db->query("insert into frg_bak_task (type,uid,lang,partner,server,task_start_time,status) VALUES ('man',".$_SESSION['id'].",'".$task['lang']."','".$task['partner']."','".$task['server']."',now(),1)");
								$tmp = json_decode($row['extrainfo']);
								if($output_tmp[$row['ip'].":".$row['sshport']]){
									$output_tmp[$row['ip'].":".$row['sshport']]=$output_tmp[$row['ip'].":".$row['sshport']].' '.$tmp->dbname;
								}else{
									$output_tmp[$row['ip'].":".$row['sshport']]="' ".$tmp->dbname;
								}
							}
						}
					} else {
						echo 'fail';
					}
				}
				foreach ($output_tmp as $key => $value){
						$output .= "frgbak_task:".$key.":".$value." '\n";
				}
				$today = date("Ymd");
                if(!is_dir(frg_bak_task_dir."/".$today)) {
                    mkdir(frg_bak_task_dir."/".$today);
                }
				file_put_contents(frg_bak_task_dir."/".$today."/".$db->insert_id, $output);
				if (copy(frg_bak_task_dir."/".$today."/".$db->insert_id,"/data/www/EBAK/jobpush/newtask/".$db->insert_id)) {
					echo "success";
					die();
				}
				#system("/usr/bin/scp -i /data/www/EBAK/key -o StrictHostKeyChecking=no  ".frg_bak_task_dir."/".$today."/".$db->insert_id."  support@121.10.140.91:/usr/local/cndw_api/newtask/");
			}else{
				echo "Fail";
				die();
			}
		break;
		case 'frg_bak_list':
			session_start();
			if($_SESSION['id'] != '') {
				extract($_REQUEST);
                $extrmsql = '';
                if ($sort){
                    $extrmsql_sort .= ' order by ';
                    $sort = json_decode(stripslashes($sort));
                    foreach ($sort as $tmp){
                        $extrmsql_sort .= $tmp->property.' '.$tmp->direction.',';
                    }
                    $extrmsql_sort = substr($extrmsql_sort, 0, -1);
                }
                if($myself == 'true'){
                    $extrmsql_myself = " uid = ".$_SESSION['id']." ";
                }
                if($today == 'true'){
                    $extrmsql_today = " TO_DAYS(task_start_time) = TO_DAYS(NOW()) ";
                }
                if($frgbaktype == 'auto'){
                    $extrmsql_frgbaktype = " type = 'auto' ";
                }elseif($frgbaktype == 'man'){
                    $extrmsql_frgbaktype = " type = 'man' ";
                }
				if($status == 1){
					$extrmsql_status = " status = 1 ";	
				}elseif($status == 2){
					$extrmsql_status = " status = 2 ";	
				}elseif($status == 3){
					$extrmsql_status = " status = 3 ";	
				}elseif($status == 4){
					$extrmsql_status = " status = 4 ";	
				}elseif($status == 5){
					$extrmsql_status = " status = 5 ";	
				}elseif($status == 6){
					$extrmsql_status = " status = 6 ";	
				}elseif($status == 255){
					$extrmsql_status = " status = 255 ";	
				}
                if(($myself != 'false')||($today != 'false')||($frgbaktype != 'all')||($status != 'all')){
                    $extrmsql = "where ".(($myself != 'false')?$extrmsql_myself:" ").(($myself != 'false')&&($today != 'false')?" and ":" ").(($today != 'false')?$extrmsql_today:" ").((($frgbaktype != 'all')&&($today != 'false'))||(($myself != 'false')&&($frgbaktype != 'all'))?" and ":" ").(($frgbaktype != 'all')?$extrmsql_frgbaktype:" ").(((($status != 'all')&&($frgbaktype != 'all'))||(($status != 'all')&&($today != 'false'))||(($status != 'all')&&($myself != 'false')))?" and ":" ").(($status != 'all')?$extrmsql_status:" ");
                }
				$results=$db->get_results("select * from frg_bak_task ".$extrmsql.$extrmsql_sort." limit ".$start.",".$limit);
				$num_rows = $db->num_rows;
				$total = $db->get_var("select count(*) from frg_bak_task ".$extrmsql.$extrmsql_sort);
				$output = array();
				if ($num_rows > 0){
					foreach ($results as $result){
						$row = array();
						$row['id'] = $result->id;
						$row['lang'] = $result->lang;
						$row['partner'] = $result->partner;
						$row['server'] = $result->server;
						$row['filename'] = $result->filename;
						$row['task_start_time'] = $result->task_start_time;
						$row['process_start_time'] = $result->process_start_time;
						$row['dump_start_time'] = $result->dump_start_time;
						$row['dump_end_time'] = $result->dump_end_time;
						$row['zip_start_time'] = $result->zip_start_time;
						$row['zip_end_time'] = $result->zip_end_time;
						$row['transport_start_time'] = $result->transport_start_time;
						$row['transport_end_time'] = $result->transport_end_time;
						$row['end_time'] = $result->end_time;
						$row['unzip_size'] = $result->unzip_size;
						$row['zip_size'] = $result->zip_size;
						$row['log'] = $result->log;
						$row['status'] = $result->status;
						array_push($output,$row);
					}
				}
				header("Cache-Control: no-cache");
                echo json_encode(array('total'=>$total,'frg_bak_list' => $output));
			}
			
		break;
		case 'frg_bak_progressbar':
			session_start();
			if($_SESSION['id'] != '') {
				extract($_REQUEST);
				if($myself == 'true'){
                    $extrmsql_myself = " uid = ".$_SESSION['id']." ";
                }
                if($today == 'true'){
                    $extrmsql_today = " TO_DAYS(task_start_time) = TO_DAYS(NOW()) ";
                }
                if($frgbaktype == 'auto'){
                    $extrmsql_frgbaktype = " type = 'auto' ";
                }elseif($frgbaktype == 'man'){
                    $extrmsql_frgbaktype = " type = 'man' ";
                }
                if($status == 1){
					$extrmsql_status = " status = 1 ";	
				}elseif($status == 2){
					$extrmsql_status = " status = 2 ";	
				}elseif($status == 3){
					$extrmsql_status = " status = 3 ";	
				}elseif($status == 4){
					$extrmsql_status = " status = 4 ";	
				}elseif($status == 5){
					$extrmsql_status = " status = 5 ";	
				}elseif($status == 6){
					$extrmsql_status = " status = 6 ";	
				}elseif($status == 255){
					$extrmsql_status = " status = 255 ";	
				}
                if(($myself != 'false')||($today != 'false')||($frgbaktype != 'all')||($status != 'all')){
                    $extrmsql = "where ".(($myself != 'false')?$extrmsql_myself:" ").(($myself != 'false')&&($today != 'false')?" and ":" ").(($today != 'false')?$extrmsql_today:" ").((($frgbaktype != 'all')&&($today != 'false'))||(($myself != 'false')&&($frgbaktype != 'all'))?" and ":" ").(($frgbaktype != 'all')?$extrmsql_frgbaktype:" ").(((($status != 'all')&&($frgbaktype != 'all'))||(($status != 'all')&&($today != 'false'))||(($status != 'all')&&($myself != 'false')))?" and ":" ").(($status != 'all')?$extrmsql_status:" ");
                }
				$results=$db->get_results("select id , status from frg_bak_task ".$extrmsql);
				$output = array();
				if ($db->num_rows > 0){
					foreach ($results as $result){
						$output[$result->id] = $result->status;
					}
				}
				header("Cache-Control: no-cache");
                echo json_encode($output);
			}
		break;
		case 'frg_bak_man':
			extract($_REQUEST);
			//if (in_array($key,array(md5((date("YmdHi")-1).'MISKEY'),md5(date("YmdHi").'MISKEY'),md5((date("YmdHi")+1).'MISKEY')))){
			if ($key == 'yvlejpu9yrmkxpsgz0nml0ug8yfk41y9'){
				if ($act == 'proc_start'){
					$db->query("update frg_bak_task 
									set process_start_time = now() , status = 2
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'dump_start'){
					$db->query("update frg_bak_task 
									set dump_start_time = now() , status = 3
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'dump_end'){
					$db->query("update frg_bak_task 
									set dump_end_time = now() , unzip_size = '".$unzip_size."'
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'zip_start'){
					$db->query("update frg_bak_task 
									set zip_start_time = now()  , status = 4
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'zip_end'){
					$db->query("update frg_bak_task 
									set zip_end_time = now() , zip_size = '".$zip_size."' , filename = '".$filename."'
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'tran_start'){
					$db->query("update frg_bak_task 
									set transport_start_time = now()  , status = 5
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'tran_end'){
					$db->query("update frg_bak_task 
									set transport_end_time = now() 
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'proc_end'){
					$db->query("update frg_bak_task 
									set end_time = now()  , status = 6
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'error'){
					$db->query("update frg_bak_task 
									set status = 255 , log = '".$log."'
									where type = 'man' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}
			}
		break;
		case 'frg_bak_auto':
			extract($_REQUEST);
			//if (in_array($key,array(md5((date("YmdHi")-1).'MISKEY'),md5(date("YmdHi").'MISKEY'),md5((date("YmdHi")+1).'MISKEY')))){
			if ($key == 'yvlejpu9yrmkxpsgz0nml0ug8yfk41y9'){
				if ($act == 'proc_start'){
					$tmp = $db->get_row("select lang,partner from frg_bak_map where target = '".$hostname."' limit 1");
					$db->query("insert into frg_bak_task (type,lang,partner,server,task_start_time,process_start_time,status) 
								VALUES ('auto','".$tmp->lang."','".$tmp->partner."','".$server."',now(),now(),2)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'dump_start'){
					$db->query("update frg_bak_task 
									set dump_start_time = now() , status = 3
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'dump_end'){
					$db->query("update frg_bak_task 
									set dump_end_time = now() , unzip_size = '".$unzip_size."'
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'zip_start'){
					$db->query("update frg_bak_task 
									set zip_start_time = now()  , status = 4
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'zip_end'){
					$db->query("update frg_bak_task 
									set zip_end_time = now() , zip_size = '".$zip_size."' , filename = '".$filename."'
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'tran_start'){
					$db->query("update frg_bak_task 
									set transport_start_time = now()  , status = 5
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'tran_end'){
					$db->query("update frg_bak_task 
									set transport_end_time = now() 
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'proc_end'){
					$db->query("update frg_bak_task 
									set end_time = now()  , status = 6
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}elseif($act == 'error'){
					$db->query("update frg_bak_task 
									set status = 255 , log = '".$log."'
									where type = 'auto' and partner = (select partner from frg_bak_map where target = '".$hostname."') 
										and server = '".$server."' 
										and TO_DAYS(task_start_time) = TO_DAYS(NOW())
										and status <> (6|255)");
					if ($db->rows_affected > 0){
						echo "success";
						die();
					}
				}
			}
		break;
	}


?>
